package com.moleq.dialog;

import java.util.List;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.mgpos.activities.R;
import com.moleq.models.PrinterSetting;
import com.moleq.util.MoleQUtil;

public class PrinterEditDialog extends Dialog implements OnResultListener {

	private OnResultListener mOnResultListener;
	private Button btn_printer_ok,btn_printer_cancel;
	private EditText et_printerno, et_printername, et_printerip, et_printerport;
	private LinearLayout ll_dialog_perview_mode_layout;
	private Context mContext;
	private List<PrinterSetting> listPrinter;
	private PrinterSetting mPrinterSetting;
	private MessageDialog mMessageDialog;
	private String action = "";
	
	public void onCloseWithReturn(int what,Object obj){
		mOnResultListener.onReturnValue(what,obj);
		dismiss();
	}

	public PrinterEditDialog(Context context, Object base) {
		super(context,R.style.dialog2);
		constructor(context,base);
	}
	
	public void setSource(List<PrinterSetting> listPrinter,PrinterSetting printer,String action){
		this.listPrinter = listPrinter;
		this.mPrinterSetting = printer;
		this.action = action;
	}
	private void constructor(Context context,Object base) {
		mContext = context;
		mOnResultListener = (OnResultListener) base;
	}

	public PrinterEditDialog(Context context, int theme) {
		super(context, theme);
		constructor(context);
	}
	
	private void constructor(Context context) {
		// TODO Auto-generated method stub
		mContext = context;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.ps_dialog_edit_printer);
		this.setCancelable(false);
	
		ll_dialog_perview_mode_layout = (LinearLayout) findViewById(R.id.ll_dialog_perview_mode_layout);
		et_printerno = (EditText) findViewById(R.id.et_printerno);
		et_printername = (EditText) findViewById(R.id.et_printername);
		et_printerip = (EditText) findViewById(R.id.et_printerip);
		et_printerport = (EditText) findViewById(R.id.et_printerport);
		
		btn_printer_ok = (Button) findViewById(R.id.btn_printer_ok);
		btn_printer_cancel = (Button) findViewById(R.id.btn_printer_cancel);
		ll_dialog_perview_mode_layout.setOnClickListener(OnClickDialogWindowListener);
		btn_printer_ok.setOnClickListener(OnClickDialogWindowListener);
		btn_printer_cancel.setOnClickListener(OnClickDialogWindowListener);
	}
	
	public void show(){
		super.show();	
		getDetail(mPrinterSetting);
		et_printername.requestFocus();
	}
	
	public void getDetail(PrinterSetting printer){
//		et_printerno.setText(String.valueOf(printer.getPs_id()));
		et_printerno.setText(String.valueOf(printer.getNo()));
		et_printername.setText(printer.getPs_name());
		et_printerip.setText(printer.getPs_ip());
		int port = printer.getPs_port();
		if (port != 0) {
			et_printerport.setText(String.valueOf(port));
		}else {
			et_printerport.setText("");
		}
	}
	
	public Button.OnClickListener OnClickDialogWindowListener = new Button.OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_printer_ok:
				if (validation() == false) return;
				
				mPrinterSetting.setPs_name(et_printername.getText().toString().trim());
				mPrinterSetting.setPs_ip(et_printerip.getText().toString().trim());
				if (et_printerport.getText().toString().trim().equals("")) {
					mPrinterSetting.setPs_port(0);
				}else {
					mPrinterSetting.setPs_port(Integer.valueOf(et_printerport.getText().toString()));
				}
				if (mPrinterSetting.getType().equals("I") && action.equals("add")) {
					listPrinter.add(mPrinterSetting);
				}
				mPrinterSetting.setEdit(true);
				
				onCloseWithReturn(R.id.btn_printer_ok, listPrinter);
				dismiss();
				break;
			case R.id.btn_printer_cancel:
				dismiss();
				break;
			case R.id.ll_dialog_perview_mode_layout:
				InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
				inputMethodManager.hideSoftInputFromWindow(PrinterEditDialog.this
						.getCurrentFocus().getWindowToken(),
						InputMethodManager.HIDE_NOT_ALWAYS);
				break;
			}
		}
	};
	
	public boolean validation(){
		boolean bo = true;
		String ip = et_printerip.getText().toString().trim();
		if (et_printername.getText().toString().trim().equals("")) {
			ShowMessage("Name can not be empty.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			et_printername.requestFocus();
			bo = false;
		}else if (ip.equals("")) {
			ShowMessage("Ip can not be empty.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			et_printerip.requestFocus();
			bo = false;
		}else if (!MoleQUtil.checkIp(ip)) {
			ShowMessage("Ip address is not valid.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			et_printerip.requestFocus();
			bo = false;
		}
		return bo;
	}
	
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener(){

		public void onClickYes() {
			
		}

		public void onClickNo() {
			mMessageDialog.dismiss();
		}

		public void onClickOK() {
			mMessageDialog.dismiss();
		}
	};
	
	public void ShowMessage(String text,int type,int icon){
		mMessageDialog = new MessageDialog(mContext);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}

	@Override
	public void onCloseWithReturn(Class<? extends MoleQBaseDialog> pramClass,
			int viewId, String returnvalue) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onReturnValue(int what, Object obj) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onShowingMessageDlg(boolean isShowing) {
		// TODO Auto-generated method stub
		
	}
}
